package com.rainbow.micro.example.controller;

import com.github.pagehelper.PageInfo;
import com.rainbow.common.core.entity.Result;
import com.rainbow.micro.example.dto.req.RoleQueryReq;
import com.rainbow.micro.example.dto.req.RoleStoreReq;
import com.rainbow.micro.example.dto.resp.RoleResp;
import com.rainbow.micro.example.service.RoleService;
import com.rainbow.support.action.log.annotation.ActionLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;


/**
* role管理
* 
* @author rainbow
* 邮箱  1335157415@qq.com
* @date 2022-04-30 15:10:42
*/
@RestController
@RequestMapping("/role")
@Api(tags = "示例管理")
@RequiredArgsConstructor
public class RoleController{

    private final RoleService roleService;


    /**
    * 分页查询
    * @param entity 查询参数
    * @return 查询结果
    */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询", httpMethod = "POST",response = RoleResp.class)
    @ActionLog(moudle = "管理",actionType = "分页查询")
    public PageInfo<RoleResp> page(@RequestBody RoleQueryReq entity){
        Assert.notNull(entity,"参数不允许为空");
        return roleService.page(entity);
    }


    /**
    * 保存数据
    * @param entity 参数
    * @return 保存结果
    */
    @PostMapping("/insert")
    @ActionLog(moudle = "管理",actionType = "创建")
    @ApiOperation(value = "创建数据", httpMethod = "POST",response = Result.class)
    public Result insert(@ApiParam(name = "对象", value = "传入json格式", required = true) @RequestBody RoleStoreReq entity) {
        Assert.notNull(entity,"参数不允许为空");
        return Result.success(roleService.save(entity));
    }


    /**
    * 更新数据
    * @param entity 对象参数
    * @return 更新结果
    */
    @PostMapping("/update")
    @ActionLog(moudle = "管理",actionType = "更新")
    @ApiOperation(value = "更新数据", httpMethod = "POST",response = Result.class)
    public Result update(@ApiParam(name = "对象", value = "传入json格式", required = true)  @RequestBody RoleStoreReq entity) {
        Assert.notNull(entity,"参数不允许为空");
        return Result.success(roleService.update(entity));
    }


    /**
    * 逻辑删除
    * @param entity 对象参数
    * @return 删除结果
    */
    @PostMapping("/delete")
    @ActionLog(moudle = "管理",actionType = "逻辑删除")
    @ApiOperation(value = "逻辑删除", httpMethod = "POST",response = Boolean.class)
    public Boolean delete(@ApiParam(name = "对象", value = "传入json格式", required = true)  @RequestBody RoleStoreReq entity) {
        Assert.notNull(entity,"参数不允许为空");
        return roleService.delete(entity);
    }

    /**
     * 导入excel
     * @param file 文件
     * @return
     */
    @PostMapping(value = "/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "导入excel",notes = "导入excel", httpMethod = "POST",response = Result.class)
    public Result upload(@RequestPart(value = "file") MultipartFile file) {
        Assert.notNull(file,"excel文件不能为空!");
        return roleService.upload(file);
    }

    /**
     * 导出excel
     * @return
     */
    @PostMapping(value = "/export")
    @ApiOperation(value = "导出excel",notes = "导出excel", httpMethod = "POST",response = Result.class)
    public Result export(HttpServletResponse response, @RequestBody RoleQueryReq req) {
        return roleService.export(response,req);
    }

    /**
     * 导入excel
     * @param file 文件
     * @return
     */
    @PostMapping(value = "/uploadTxt", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ApiOperation(value = "导入excel",notes = "导入excel", httpMethod = "POST",response = Result.class)
    public Result uploadTxt(@RequestPart(value = "file") MultipartFile file) {
        Assert.notNull(file,"txt文件不能为空!");
        return roleService.uploadTxt(file);
    }

}
